package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void sendVerifyCode(String phone, String code) {
        //设计key,见名知意
        String key = RedisKeys.VERIFY_CODE.join(phone);
        //存放到redis中,设置5分钟过期
        template.opsForValue().set(key,code,RedisKeys.VERIFY_CODE.getTime(),TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {

        String key = RedisKeys.VERIFY_CODE.join(phone);

        return template.opsForValue().get(key);
    }

    @Override
    public String setToken(UserInfo userInfo) {

        //生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //设计登录的key
        String key = RedisKeys.USER_LOGIN_LOKEN.join(token);
        //用户对象作为value
        String value = JSON.toJSONString(userInfo);
        //存放到redis缓存中,存活时间30分钟
        template.opsForValue().set(key,value,RedisKeys.USER_LOGIN_LOKEN.getTime(),TimeUnit.SECONDS);
        return token;
    }

    @Override
    public UserInfo getUserByToken(String token) {
        //判断token是否为空
        if(!StringUtils.hasLength(token)){
           return null;
        }
        String key = RedisKeys.USER_LOGIN_LOKEN.join(token);
        if(template.hasKey(key)){
            String userStr = template.opsForValue().get(key);
            UserInfo userInfo = JSON.parseObject(userStr, UserInfo.class);
            //重置token时间
            template.expire(key,RedisKeys.USER_LOGIN_LOKEN.getTime(),TimeUnit.SECONDS);
            userInfo.setPassword("");
            return userInfo;
        }
        return null;
    }

    @Override
    public List<String> getUserStrategyFavorList(String uid) {
        String key = RedisKeys.USER_STRATEGY_FAVOR_LIST.join(uid);
        List<String> list = null;
        //判断Redis中是否有该key
        if(template.hasKey(key)){
            //如果有,则之间拿出来
            String strList = template.opsForValue().get(key);
            list = JSON.parseArray(strList, String.class);
        }else{
            //如果没有,初始化
            list = userInfoService.get(uid).getFavorStrategyList();
        }
        return list;
    }

    @Override
    public void setUserStrategyFavorList(UserInfo userInfo,List<String> list) {
        String key = RedisKeys.USER_STRATEGY_FAVOR_LIST.join(userInfo.getId());
        template.opsForValue().set(key,JSON.toJSONString(list));
    }

    @Override
    public boolean isUserStrategyFavorListExist(String uid) {
        String key = RedisKeys.USER_STRATEGY_FAVOR_LIST.join(uid);
        return template.hasKey(key);
    }

    @Override
    public List<UserInfo> queryUserFavorStrategyList() {
        Set<String> keys = template.keys(RedisKeys.USER_STRATEGY_FAVOR_LIST.join("*"));
        List<UserInfo> userInfos = new ArrayList<>();
        for (String key : keys) {
            String list = template.opsForValue().get(key);
            List<String> userFavorStrategyList = JSON.parseArray(list, String.class);
            String userId = key.split(":")[1];
            UserInfo userInfo = userInfoService.get(userId);
            userInfo.setFavorStrategyList(userFavorStrategyList);
            userInfos.add(userInfo);
        }
        return userInfos;
    }
}
